import { queryTotal, queryStatisticalIndex, queryPersonnelDetail, createPersonnel, modifyPersonnel } from './service';
import { queryBasicIntro, modifyBasicIntro, queryDocmentPage, createDocment, modifyDocment } from './service';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';
import { queryDictList } from '@/services/dict';
import { message } from 'antd';

export enum CurrentPage {
  First,
  Second,
  Third
}

export enum Operation {
  None,
  Edit,
  Add,
}
const ADD_KEY = 'Add key';

export default {
  namespace: 'archives_unit',

  state: {
    saveState: {
      needSave: false,
      currentPage: CurrentPage.First
    },
    // 基础信息
    basicIntro: '',
    // 审批资料列表页
    documentCond: {},
    list: { data: [], dataCnt: 0 },
    dPageCommitData: {
      operation: Operation.None,
      data: {}
    },
    editingKey: '',

    //
    total: {},
    details: [],
    personDetail: {}
  },

  effects: {
    *queryTotal(_, { call, put }) {
      const response = yield call(queryTotal);
      yield put({ type: 'setTotal', payload: response });
    },
    *queryStatisticalIndex(_, { call, put }) {
      const response = yield call(queryStatisticalIndex);
      yield put({ type: 'setStatisticalIndex', payload: response });
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *createPersonnel({ payload }, { call, put }) {
      const response = yield call(createPersonnel, payload);
      response ? message.success('操作成功', 1.5) : message.error('操作失败', 1.5);
    },

    *queryBasicIntro({ payload }, { call, put }) {
      const dict = yield call(queryBasicIntro, payload);
      yield put({ type: 'setBasicIntro', payload: dict.intro })
    },
    *saveFirstPage({ payload }, { call, put, select }) {
      let intro = yield select(state => state.archives_unit.basicIntro);

      const dict = yield call(modifyBasicIntro, intro);

      yield put({ type: 'updateState', payload: { saveState: {} } })
    },
    *queryDocumentPage({ payload = { pageNum: 1, pageSize: 10 } }, { call, put, select }) {
      const cond = yield select(state => state.archives_unit.documentCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryDocmentPage, params);
      yield put({ type: 'setDocumentPage', payload: { list, total } })

      // const dict = yield call(queryBasicIntro, payload);
      // yield put({ type: 'setBasicIntro', payload: dict.intro })
    },
    *updateDocumentCond({ payload }, { put, select }) {
      const cond = yield select(state => state.archives_unit.documentCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'updateState', payload: { documentCond: params } });
      yield put({ type: 'queryDocumentPage' })
    },
    *cancelEdit({ payload }, { put, select }) {
      const dPageCommitData = yield select(state => state.archives_unit.dPageCommitData);
      if (dPageCommitData.operation = Operation.Add) {
        const listData = yield select(state => state.archives_unit.list);
        listData.data.splice(0, 1);
        listData.dataCnt -= 1;

        yield put({
          type: 'updateState', payload: {
            editingKey: '',
            list: listData,
          }
        });
        yield put({ type: 'cleanDPageCommitData' });
        return;
      }

      yield put({ type: 'updateState', payload: { editingKey: '' } });

      yield put({ type: 'queryDocumentPage' })
    },
    *saveDPage({ payload }, { call, put, select }) {
      const data = yield select(state => state.archives_unit.dPageCommitData);
      // const params = { ...payload, ...cond };
      if (data.operation == Operation.Add) {
        try {
          yield call(createDocment, data.data);
          yield put({ type: 'queryDocumentPage' })
          return true;
        } catch (error) {
          // yield put({ type: 'cancelEdit' })
          throw error;
        }

      } else {

        const id = yield select(state => state.archives_unit.editingKey);
        const listData = yield select(state => state.archives_unit.list.data);

        yield call(modifyDocment, {
          ...data.data,
          id
        });

        const newData = [...listData];
        const index = newData.findIndex(item => id === item.id);
        const item = newData[index];
        newData.splice(index, 1, {
          ...item,
          ...data.data,
        });

        yield put({
          type: 'setDocumentPage', payload: {
            list: newData
          }
        });
      }

      yield put({ type: 'cleanDPageCommitData' });

      // const dict = yield call(queryBasicIntro, payload);
      // yield put({ type: 'setBasicIntro', payload: dict.intro })
    },
    *queryPersonnelDetail(_, { call, put }) {
      const response = yield call(queryPersonnelDetail);
      yield put({ type: 'setPersonnel', payload: response });
    },
    *modifyPersonnel({ payload }, { call, put }) {
      const response = yield call(modifyPersonnel, payload);
      response ? message.success('操作成功', 1.5) : message.error('操作失败', 1.5);
    }
  },

  reducers: {
    cleanDPageCommitData(state, action) {
      return {
        ...state,
        editingKey: '',
        dPageCommitData: {},
      }
    },
    editDocument(state, action) {
      return {
        ...state,
        editingKey: action.payload.key,
        dPageCommitData: {
          operation: Operation.Edit,
          data: action.payload.data
        },
      }
    },
    addNewRow(state, action) {
      const newData = {
        id: ADD_KEY,
        content: null,
        name: null,
        certificateNo: null,
        auditTime: null,
        url: null,

      };

      let listData = state.list.data || [];
      listData = [newData, ...listData];

      return {
        ...state,
        list: {
          data: listData,
          dataCnt: state.list.dataCnt + 1,
        },
        editingKey: ADD_KEY,
        dPageCommitData: {
          operation: Operation.Add,
          data: newData
        },
      }

    },
    setDocumentPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total ? action.payload.total : state.list.dataCnt,
        }
      }
    },
    updateDPageCommitData(state, action) {
      return {
        ...state,
        // ...action.payload,
        dPageCommitData: {
          ...state.dPageCommitData,
          data: action.payload,
          // operation: Operation.None,
          // data: {}
        },
      }
    },
    updateState(state, action) {
      return {
        ...state,
        ...action.payload,
      }
    },
    setBasicIntro(state, action) {
      return {
        ...state,
        basicIntro: action.payload
      }
    },
    setTotal(state, action) {
      return {
        ...state,
        total: action.payload
      }
    },
    setStatisticalIndex(state, action) {
      return {
        ...state,
        statisticalIndex: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setPersonnel(state, action) {
      return {
        ...state,
        personDetail: action.payload
      }
    }
  }
}
